@extends('manage.layout')

@section('content')
    <section class="content-header">
        <h1>
            编辑产品&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/product" class="btn btn-primary">返回</a>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Product</a></li>
            <li class="active">Product</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"></h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="/product/productUpdate" method="post" role="form" style="width: 50%;" enctype="multipart/form-data">
                        {{csrf_field()}}
                        <input type="hidden" name="id" value="{{$product->id}}" />
                        <div class="box-body">
                            <div class="form-group">
                                <label for="exampleInputEmail1">一级分类</label>
                                <select id="setTwo" name="parent_id" class="form-control">
                                    @foreach($one as $o)
                                        <option value="{{ $o['id'] }}" @if($o['id'] == $category_one->id) selected="selected" @endif>{{ $o['name'] }}</option>
                                    @endforeach
                                </select>
                            </div>
							<div class="form-group">
                                <label for="exampleInputEmail1">二级分类</label>
                                <select id='category_two' name="category" class="form-control">
                                    @foreach($two as $t)
                                        <option value="{{ $t['id'] }}" @if($t['id'] == $category_two->id) selected="selected" @endif>{{ $t['name'] }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品名称</label>
                                <input type="text" name="name" class="form-control" value="{{$product->name}}" placeholder="Product Name" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">权重</label>
                                <input type="number" name="rank" min="0" class="form-control" value="{{$product->rank}}" placeholder="Product rank" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品估价</label>
                                {{--<select name="price" class="form-control">--}}
                                    {{--<option value="0-5000" @if($product->price == '0-5000')selected="selected"@endif>0-5000</option>--}}
                                    {{--<option value="5000-10000" @if($product->price == '5000-10000')selected="selected"@endif>5000-10000</option>--}}
                                    {{--<option value="10000-20000" @if($product->price == "10000-20000")selected="selected"@endif>10000-20000</option>--}}
                                    {{--<option value="20000-50000" @if($product->price == "20000-50000")selected="selected"@endif>20000-50000</option>--}}
                                    {{--<option value="50000以上" @if($product->price == "50000以上")selected="selected"@endif>50000以上</option>--}}
                                {{--</select>--}}
                                <input type="text" name="price" class="form-control" value="{{$product->price}}" placeholder="Product price" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">制作周期</label>
                                <input type="text" name="period" class="form-control" value="{{$product->period}}" placeholder="Product period" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">绩效系数</label>
                                <input type="text" name="performance" class="form-control" value="{{$product->performance}}" placeholder="Product performance" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">适用企业</label>
                                <input type="text" name="firm" class="form-control" value="{{$product->firm}}" placeholder="Product period" required="required">
                            </div>

                            {{--<div class="form-group">--}}
                                {{--<label for="exampleInputPassword1">产品图片</label>--}}
                                {{--<input type="file" name="picture" class="form-control">--}}
                            {{--</div>--}}
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品简介</label>
                                <textarea name="brief" class="form-control" placeholder="Product brief" required="required">{{$product->brief}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">服务细则</label>
                                <textarea name="content" style="width:100%;height:300px;">{{$product->content}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">备注</label>
                                <textarea name="remark" class="form-control" placeholder="Product remark" required="required">{{$product->remark}}</textarea>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
@stop

@section('script')
    <script>
        $(function() {
            //初始化一二级分类
            ajax('{{$category_one->id}}');
			//富文本编辑器
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
                    afterBlur: function () { this.sync(); },
					allowFileManager : true
				});
			});
			//提交表单z'c
            $("form").submit(function(event) {
                event.preventDefault();
                var url = $(this).attr("action");
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: url,
                    type: "POST",
                    data: $("form").serialize(),
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        if(data.code == 100) {
                            window.location.href = "/product";
                            layer.msg(data.msg)
                        }
                        else {
                            layer.msg(data.msg)
                        }
                    }
                });
            });
			//根据一级分类获取二级分类
			$("#setTwo").change(function(){
				var _id = $("#setTwo").val();
				ajax(_id);
			});
        });
        function ajax(_id){
            $.ajax({
                url: '/product/getTwo',
                type: "GET",
                data: {parent_id:_id},
                dataType: "json",
                success: function(data) {
                    console.log(data);
                    if(data.code == 100) {
                        $('#category_two').empty();
                        var html = '';
                        for(var i=0;i<data['category_two'].length;i++){
                            html+='<option value="'+data['category_two'][i]['id']+'"';
                            if('{{$category_two->id}}' == data['category_two'][i]['id']){
                                html +='selected="selected"';
                            }
                            html+='>'+data['category_two'][i]['name']+'</option>';
                        }
                        $('#category_two').html(html);
                    }else{
                        layer.msg(data.msg);
                    }
                }
            });
        }
    </script>

@stop